class HealthEntriesController < ApplicationController
  # GET /health_entries
  # GET /health_entries.xml
  def index
    @health_entries = HealthEntry.all

    respond_to do |format|
      format.html # index.html.erb
      format.xml  { render :xml => @health_entries }
    end
  end

  # GET /health_entries/1
  # GET /health_entries/1.xml
  def show
    @health_entry = HealthEntry.find(params[:id])

    respond_to do |format|
      format.html # show.html.erb
      format.xml  { render :xml => @health_entry }
    end
  end

  # GET /health_entries/new
  # GET /health_entries/new.xml
  def new
    @health_entry = HealthEntry.new
    @health_entry.created_at = DateTime.now
    
    respond_to do |format|
      format.html # new.html.erb
      format.xml  { render :xml => @health_entry }
    end
  end

  # GET /health_entries/1/edit
  def edit
    @health_entry = HealthEntry.find(params[:id])
  end

  # POST /health_entries
  # POST /health_entries.xml
  def create
    @health_entry = HealthEntry.new
    @blood_sugar = BloodSugar.new(:value => params[:health_entry][:blood_sugar].to_f)
    @insulin_dose = InsulinDose.new(:value => params[:health_entry][:insulin_dose].to_i)
    
    @health_entry.blood_sugar = @blood_sugar
    @health_entry.insulin_dose = @insulin_dose
    
    respond_to do |format|
      if @health_entry.save
        flash[:notice] = 'HealthEntry was successfully created.'
        format.html { redirect_to(@health_entry) }
        format.xml  { render :xml => @health_entry, :status => :created, :location => @health_entry }
      else
        format.html { render :action => "new" }
        format.xml  { render :xml => @health_entry.errors, :status => :unprocessable_entity }
      end
    end
  end

  # PUT /health_entries/1
  # PUT /health_entries/1.xml
  def update
    @health_entry = HealthEntry.find(params[:id])

    respond_to do |format|
      if @health_entry.update_attributes(params[:health_entry])
        flash[:notice] = 'HealthEntry was successfully updated.'
        format.html { redirect_to(@health_entry) }
        format.xml  { head :ok }
      else
        format.html { render :action => "edit" }
        format.xml  { render :xml => @health_entry.errors, :status => :unprocessable_entity }
      end
    end
  end

  # DELETE /health_entries/1
  # DELETE /health_entries/1.xml
  def destroy
    @health_entry = HealthEntry.find(params[:id])
    @health_entry.destroy

    respond_to do |format|
      format.html { redirect_to(health_entries_url) }
      format.xml  { head :ok }
    end
  end
end
